package org.btik.light.mobile.meta.mo;

import org.btik.light.meta.DevMo;
import org.btik.light.meta.DevState;
import org.btik.light.mobile.meta.DevPanelType;
import org.btik.light.mobile.mo.DevMoFactory;
import org.btik.light.mobile.mo.MoCreator;


import java.util.HashMap;
import java.util.Map;

import static org.btik.light.meta.LightDevAttrConstant.DEV_TYPE;

/**
 * 设备管理对象工厂
 *
 * @author lustre
 * @since 2022/6/28 15:48
 */

public class DefaultDevMoFactory implements DevMoFactory {
    private static final HashMap<String, MoCreator> CREATORS = new HashMap<>();


    public DevMo newDev(String sn, Map<String, String> attr) {
        String devType = attr.get(DEV_TYPE);
        MoCreator creator = CREATORS.get(devType);
        if (creator == null) {
            return defaultDevMo(sn, attr);
        }
        return creator.create(sn, attr);
    }

    public static DefaultDevMo defaultDevMo(String sn, Map<String, String> attr) {
        DefaultDevMo defaultDevMo = new DefaultDevMo(sn, DevState.CONNECTED);
        defaultDevMo.setExtendedAttributes(attr);
        defaultDevMo.setDevPanelType(DevPanelType.Fragment);
        return defaultDevMo;
    }

    public static DevPanelType getPanelType(DevMo devMo) {
        String devType = devMo.getDevType();
        MoCreator moCreator = CREATORS.get(devType);
        if (moCreator == null) {
            System.err.println(devType);
            return null;
        }
        if (moCreator instanceof DevPanelTypeOwner) {
            return ((DevPanelTypeOwner) moCreator).devPanelType();
        }
        System.err.println(moCreator.getClass());
        return null;
    }

    public void regCreator(String devType, MoCreator moCreator) {
        CREATORS.put(devType, moCreator);
    }

}
